﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace RenShiKaoQin.Account
{
    public partial class SPForm : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                using (hrdbEntities ent = new hrdbEntities())
                {
                    var result = from kqemp in ent.TKQEmployees where kqemp.FEmployeeID.Equals(Page.User.Identity.Name) select kqemp;
                    if (result.Any())
                    {
                        TKQEmployee kq = result.Single();
                        if (!kq.IsExaminant)
                        {
                            Response.Write("您无权查看此页面");
                            Response.End();
                        }

                    }
                    else
                    {
                        Response.Write("您无权查看此页面");
                        Response.End();
                    }
                    DateTime now = DateTime.Now;
                    DateTime monthS = now.AddDays(-(now.Day - 1));
                    tbStart.Text = monthS.ToString("yyyy-MM-dd");
                    tbEnd.Text = monthS.AddDays(DateTime.DaysInMonth(now.Year, now.Month) - 1).ToString("yyyy-MM-dd");
                    btnSearch_Click(null, null);
                }
            }
            tbStart.Attributes["readonly"] = "readonly";
            tbEnd.Attributes["readonly"] = "readonly";
        }

        protected void gvSP_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            int serial = Convert.ToInt32(e.Keys[0]);
            using (hrdbEntities ent = new hrdbEntities())
            {
                var result = from rec in ent.TKQRequestRecords where rec.SerialNO == serial select rec;
                if (result.Any())
                {
                    TKQRequestRecord record = result.Single();
                    //TimeSpan ts = record.EndDate - record.StartDate;
                    //if (ts.TotalDays < 2)
                    //{
                    //    ClientScript.RegisterStartupScript(this.GetType(), "CannotSPTwoDays", "alert('两天以内的申请需要科室自行审批');", true);
                    //    e.Cancel = true;
                    //    return;
                    //}
                    if (record.CheckStatus == false)
                    {
                        e.Cancel = false;
                        if (!record.ExamStatus)
                        {
                            sqlSP.UpdateParameters["ExamerID"] = new Parameter("ExamerID", TypeCode.String, Page.User.Identity.Name);
                            string temp = DateTime.Now.ToString("u");
                            temp = temp.Substring(0, temp.Length - 1);
                            sqlSP.UpdateParameters["ExamDate"] = new Parameter("ExamDate", TypeCode.DateTime, temp);
                        }
                        else
                        {
                            if (record.ExamerID != User.Identity.Name)
                            {
                                ClientScript.RegisterStartupScript(this.GetType(), "CannotdeleteOthers", "alert('只能撤销自己审批的申请');", true);
                                e.Cancel = true;
                                return;
                            }
                            sqlSP.UpdateParameters["ExamerID"] = new Parameter("ExamerID", TypeCode.String, ""); 
                            sqlSP.UpdateParameters["ExamDate"] = new Parameter("ExamDate", TypeCode.DateTime, "");
                        }
                        sqlSP.UpdateParameters["ExamStatus"] = new Parameter("ExamStatus", TypeCode.Boolean,(!record.ExamStatus).ToString());
                    }
                    else
                    {
                        ClientScript.RegisterStartupScript(this.GetType(), "Cannotdelete", "alert('只有未被复核的申请才可以撤销');", true);
                        e.Cancel = true;
                    }
                }
            }
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            sqlSP.SelectParameters.Clear();
            sqlSP.SelectParameters.Add("ExamStatus", TypeCode.Boolean, cbDisplaySP.Checked.ToString());
            if (tbName.Text != "")
            {
                sqlSP.SelectParameters.Add("EmployeeName", tbName.Text);
                string tmin = DateTime.Now.AddDays(2).ToString("u");
                string tmax = DateTime.Now.AddDays(1).ToString("u");
                sqlSP.SelectParameters.Add("StartDate", TypeCode.DateTime, tmin.Substring(0, tmin.Length - 1));
                sqlSP.SelectParameters.Add("EndDate", TypeCode.DateTime, tmax.Substring(0, tmax.Length - 1));
            }
            else
            {
                sqlSP.SelectParameters.Add("EmployeeName", string.Empty);
                sqlSP.SelectParameters["EmployeeName"].ConvertEmptyStringToNull = false;
                sqlSP.SelectParameters.Add("StartDate", TypeCode.DateTime, tbStart.Text);
                sqlSP.SelectParameters.Add("EndDate", TypeCode.DateTime, tbEnd.Text);
            }
        }

        protected void btnExport_Click(object sender, EventArgs e)
        {

        }

        protected void cbDisplaySP_CheckedChanged(object sender, EventArgs e)
        {
            btnSearch_Click(null, null);
        }

        protected void gvSP_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "DispReason" || e.CommandName == "UpdateReason")
            {
                int rowIndex = int.Parse(e.CommandArgument.ToString());
                TextBox tbReason = (TextBox)gvSP.Rows[rowIndex].FindControl("tbReason");
                LinkButton lkbUpdate = (LinkButton)gvSP.Rows[rowIndex].FindControl("lkbUpdate");
                LinkButton lkbDispReason = (LinkButton)gvSP.Rows[rowIndex].FindControl("lkbDispReason");
                if (e.CommandName == "DispReason")
                {
                    tbReason.Visible = true;
                    lkbUpdate.Visible = true;
                    lkbDispReason.Visible = false;
                }
                if (e.CommandName == "UpdateReason")
                {
                    using (hrdbEntities ent = new hrdbEntities())
                    {
                        int serial = Convert.ToInt32(gvSP.DataKeys[rowIndex].Value);
                        var rrecord = from trec in ent.TKQRequestRecords where trec.SerialNO == serial select trec;
                        TKQRequestRecord rec = rrecord.Single();
                        rec.RejectStatus = true;
                        rec.RejectReason = tbReason.Text;
                        ent.SaveChanges();
                    }
                    tbReason.Visible = false;
                    lkbUpdate.Visible = false;
                    lkbDispReason.Visible = false;
                    gvSP.DataBind();
                }
            }
        }
    }
}